package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import tech.aistar.entity.Phone;
import tech.aistar.service.IPhoneService;
import java.util.List;

/**
 * Created by 幻想乡 on 2020/1/1.
 */
@Controller
public class PhoneController {
    @Autowired
    private IPhoneService phoneService;

    @GetMapping("/phone")
    public String list(String phoneName,Model model){
        //调用模糊查询的方法
        List<Phone> phoneList = phoneService.getAll(phoneName);

        //把这个数据放入到request作用域中
        model.addAttribute("phones",phoneList);

        //数据的回显 - 就是为了让输入的搜索条件也能够显示到页面中
        model.addAttribute("searchName",phoneName);

        return "list/sourch";
    }

    @GetMapping("/del")
    public String del(Integer id){

        phoneService.deleteById(id);

        return "redirect:/list";
    }


    @GetMapping("/delMore")
    public String del(String ids){

        //根据:进行切割
        String[] arr = ids.split(":");

        for(String sid:arr){
            phoneService.deleteById(Integer.valueOf(sid));
        }

        return "redirect:/list";
    }

    @GetMapping("/edit")
    public String edit(Integer id,Model model){
        //根据id进行查询db中该条产品的记录
        Phone b = phoneService.getById(id);
        //为了在修改.jsp页面中能够发现这个b
        model.addAttribute("b",b);
        return "list/edit";
    }
    @PostMapping("/list/editcl")
    public String editcl(Phone b){

        phoneService.update(b);

        return "redirect:/list";
    }
}
